package com.cskaoyan.day24.Method;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class API {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> aClass = Class.forName("com.cskaoyan.day24.Constructor.Person");
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }

        System.out.println("所有方法：");
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }

        System.out.println("获得指定的成员方法:");
        Method eat = aClass.getMethod("eat");
        System.out.println(eat);

        System.out.println("获得指定的成员方法:");
        Method eat1 = aClass.getDeclaredMethod("eat", String.class);
        System.out.println(eat1);

        System.out.println("调用无参方法:");

        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        Object o = declaredConstructor.newInstance();
        Object invoke = eat.invoke(o);
        System.out.println(invoke);

         
        System.out.println("调用有参方法:");
        eat1.setAccessible(true);
        Object 西红柿超级蛋 = eat1.invoke(o, "西红柿超级蛋");
        System.out.println(西红柿超级蛋);

    }
}
